var log = require("./../../util/loginfo").getInstand;
var landlord_race_dao = require("./../dao/landlord_race_Dao");
var gameInfo = require('./game').getInstand;

var landlord_api = {};

landlord_api.getlandlordrace = function(_info) {
    log.info("getLandlord_race_sort ");

    //有人离线
    if (!socket.userId) {
        return
    }
    var my_user_id = socket.userId;
    var nowDate = new Date();

    var start_time = nowDate.getFullYear() + "-" + (nowDate.getMonth() + 1) + "-" + nowDate.getDate() + " " + "11:00:00";
    var end_time = nowDate.getFullYear() + "-" + (nowDate.getMonth() + 1) + "-" + nowDate.getDate() + " " + "23:00:00";
    start_time = start_time.substring(0, 19);
    start_time = start_time.replace(/-/g, '/');

    end_time = end_time.substring(0, 19);
    end_time = end_time.replace(/-/g, '/');

    var new_start_time = new Date(start_time).getTime();
    var new_end_time = new Date(end_time).getTime();
    landlord_race_dao.query_race_record_for_sort({
        start_time: new_start_time,
        end_time: new_end_time
    }, function (state, rows) {
        if (!state) {
            log.err("landlord_race_dao query_race_record_for_sort failed!");
            return;
        }
        var race_user_list = [];
        var race_user_dict = {};
        for (var r in rows) {
            if (race_user_list.indexOf(rows[r].uid) == -1) {
                race_user_list.push(rows[r].uid);
                if (rows[r].iswin) {
                    race_user_dict[rows[r].uid] = {play: 1, win: 1}
                } else {
                    race_user_dict[rows[r].uid] = {play: 1, win: 0}
                }
                race_user_dict[rows[r].uid]["nick_name"] = rows[r].nick_name;
                race_user_dict[rows[r].uid]["head_url"] = rows[r].head_url
            } else {
                if (race_user_dict[rows[r].uid]["play"] < 20) {
                    race_user_dict[rows[r].uid]["play"] += 1;
                    if (rows[r].iswin) {
                        race_user_dict[rows[r].uid]["win"] += 1;
                    }
                }
            }
        }
        for (var rud in race_user_dict) {
            if (race_user_dict[rud]["play"] < 20) {
                delete race_user_dict[rud]
            }
        }

        log.info("race_user_dict ", race_user_dict);
        var my_sheng_lv = 0;   //我的胜率
        var my_pai_ming = 0;  //我的排名
        var race_win_res_dict = {};
        var res_rac_index_list = [];
        for (var rud in race_user_dict) {
            var win_key = parseInt(parseInt(race_user_dict[rud]["win"]) / parseInt(race_user_dict[rud]["play"]) * 100);
            race_win_res_dict[rud] = win_key;
            res_rac_index_list.push(win_key);

            if (parseInt(rud) == parseInt(my_user_id)) {
                my_sheng_lv = win_key
            }

        }
        log.info(res_rac_index_list);

        var new_res_rac_index_list = es6_set(JSON.parse(JSON.stringify(res_rac_index_list)));
        new_res_rac_index_list.sort(function (a, b) {
            return a - b
        });
        new_res_rac_index_list.reverse();

        for (var i in new_res_rac_index_list) {
            if (new_res_rac_index_list[i] == my_sheng_lv) {
                my_pai_ming = parseInt(i) + 1
            }
        }

        var win_1 = null;
        var win_2 = null;
        var win_3 = null;
        var win_4 = [];

        if (new_res_rac_index_list.length) {
            win_1 = new_res_rac_index_list[0];
            if (new_res_rac_index_list.length > 1) {
                win_2 = new_res_rac_index_list[1];
            }
            if (new_res_rac_index_list.length > 2) {
                win_3 = new_res_rac_index_list[2];
            }

            if (new_res_rac_index_list.length > 3) {
                for (var i = 3; i < new_res_rac_index_list.length; i++) {
                    if (new_res_rac_index_list[i] && i < 10) {
                        win_4.push(new_res_rac_index_list[i])
                    }
                }
            }
        }
        var huojianguser = [];
        var win_list = [];
        var win_user_list = [];
        //一等奖
        for (var rwr in race_win_res_dict) {
            if (race_win_res_dict[rwr] == win_1 && win_list.length < 4) {
                win_list.push({
                    user_id: rwr,
                    nick_name: race_user_dict[rwr].nick_name,
                    head_url: race_user_dict[rwr].head_url,
                    sheng_lv: win_1,
                });

                win_user_list.push(rwr)
            }
        }
        for (var rwr in race_win_res_dict) {
            if (race_win_res_dict[rwr] == win_2 && win_list.length < 4) {
                win_list.push({
                    user_id: rwr,
                    nick_name: race_user_dict[rwr].nick_name,
                    head_url: race_user_dict[rwr].head_url,
                    sheng_lv: win_2,
                })
                win_user_list.push(rwr)
            }
        }

        for (var rwr in race_win_res_dict) {
            if (race_win_res_dict[rwr] == win_3 && win_list.length < 4) {
                win_list.push({
                    user_id: rwr,
                    nick_name: race_user_dict[rwr].nick_name,
                    head_url: race_user_dict[rwr].head_url,
                    sheng_lv: win_3,
                });
                win_user_list.push(rwr)
            }
        }

        for (var rwr in race_win_res_dict) {
            if (win_4.indexOf(race_win_res_dict[rwr]) > -1 && win_list.length < 4) {
                win_list.push({
                    user_id: rwr,
                    nick_name: race_user_dict[rwr].nick_name,
                    head_url: race_user_dict[rwr].head_url,
                })
                win_user_list.push(rwr)
            }
        }
        for (var rwr in race_win_res_dict) {
            if (win_user_list.indexOf(rwr) == -1 && win_list.length < 4) {
                win_list.push({
                    user_id: rwr,
                    nick_name: race_user_dict[rwr].nick_name,
                    head_url: race_user_dict[rwr].head_url,
                })
                win_user_list.push(rwr)
            }
        }

        var now_shijianchuo = Number(new Date());

        socket.emit('getLandlord_race_sortResult', {
            result: true,
            win_list: win_list,
            my_sheng_lv: my_sheng_lv,
            my_pai_ming: my_pai_ming,
            over_time: new_end_time - now_shijianchuo,
            info: {
                pw_total: gameInfo.landload_race_dict.pw_total,
                socre_1: gameInfo.landload_race_dict.pw_award1,
                socre_2: gameInfo.landload_race_dict.pw_award2,
                socre_3: gameInfo.landload_race_dict.pw_award3,
                socre_4: gameInfo.landload_race_dict.pw_award4,
                socre_5: gameInfo.landload_race_dict.pw_award5,
            },
        });
    });
}

function es6_set(arr) {
    //es6 数组去重
    return Array.from(new Set(arr));
}

module.exports = landlord_api;